package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageDialog;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.enums.PaymentType;
import com.moleq.models.PosTender;
import com.moleq.util.MoleQUtil;

public class PoslineTenderService {
	private DatabaseHelper DBHelper;
	private MessageDialog mMessageDialog;
	Context context;
	
	public PoslineTenderService(){}
	
	public PoslineTenderService(Context context){
		this.context = context;
		this.DBHelper = new DatabaseHelper(context);
	}
	
	public void DeleteAll() {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM "+ DatabaseSchema.PoslineTender.SCHEMA_NAME);
	}
	
	public void DeleteByID(long id) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM "+ DatabaseSchema.PoslineTender.SCHEMA_NAME
				+" WHERE "+ DatabaseSchema.PoslineTender.LT_ID +" = ?",
				new Object[] { id });
	}
	
	public boolean CheckTenderInList(int function,String date){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		
		Cursor cursor = db.rawQuery("select mt_tare_no from " + DatabaseSchema.PoslineTender.SCHEMA_NAME
				+ " where pt_function = " + function + " and pt_date = '"+ date +"'", null);
		if (cursor == null) {
			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return false;
		}
		if (cursor.moveToNext()) {
			cursor.close();
			return true;
		}
		cursor.close();
		return false;
	}
	
//	public void Update(PoslineTender obj){
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
//		db.execSQL(
//				"Update " + DatabaseSchema.PoslineTender.SCHEMA_NAME + " set pt_date = ?,pt_seq_no = ?," +
//						"pt_online = ?,pt_function = ?,pt_due = ?,pt_tender = ?,pt_change = ?,pt_ref_no = ?," +
//						"update_datetime = strftime('%Y%m%d%H%M%S','now')" +
//						"pt_auth_chk = ?,pt_card_type = ?,pt_card_no = ?,pt_Auth = ? where pt_id = ? and pt_date = ?",
//				new Object[] { obj.getPt_date(), obj.getPt_seq_no(), obj.getPt_online(), 
//						obj.getPt_function(), obj.getPt_due(), obj.getPt_tender(), obj.getPt_change(),
//						obj.getPt_ref_no(), obj.getPt_auth_chk(), obj.getPt_id(), obj.getPt_date(),obj.getPt_card_type(),obj.getPt_card_no(),obj.getPt_Auth()});
//	}
	
//	public void InsertOrUpdate(PoslineTender obj){
//		if(CheckTenderInList(obj.getPt_function(), obj.getPt_date())){
//			this.Update(obj);
//		}else {
//			this.Insert(obj);
//		}
//	}
	
	public List<PosTender> FindAll(){
		List<PosTender> obj = new ArrayList<PosTender>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		
		Cursor cursor = db.rawQuery("select * from " + DatabaseSchema.PoslineTender.SCHEMA_NAME, null);
		if (cursor == null) {
			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return obj;
		}
		
		while (cursor.moveToNext()) {
			int lt_id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PoslineTender.LT_ID));
			int lt_store_id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PoslineTender.LT_STORE_ID));
			int lt_station = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PoslineTender.LT_STATION));
			String lt_date = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PoslineTender.LT_DATE));
			int lt_seq_no = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PoslineTender.LT_SEQ_NO));
			String lt_online = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PoslineTender.LT_ONLINE));
			
			int lt_function = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PoslineTender.LT_FUNCTION));
			double lt_due = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.PoslineTender.LT_DUE));
			double lt_tender = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.PoslineTender.LT_TENDER));
			double lt_change = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.PoslineTender.LT_CHANGE));
			String lt_ref_no = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PoslineTender.LT_REF_NO));
			String lt_auth_chk = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PoslineTender.LT_AUTH_CHK));
			String lt_card_type = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PoslineTender.LT_CARD_TYPE));
			String lt_card_no = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PoslineTender.LT_CARD_NO));
			String lt_Auth = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PoslineTender.LT_AUTH));
			String lt_host_code = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PoslineTender.LT_HOST_CODE));
			String trn_no = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PoslineTender.LT_TRN_NO)); 
			
			int lt_trn_no = 0;
			if (trn_no != null) {
				lt_trn_no = Integer.valueOf(trn_no);
			}
			obj.add(new PosTender(lt_id,lt_store_id, lt_station, lt_date, lt_seq_no, lt_online, lt_function, lt_due, lt_tender, lt_change, lt_ref_no,lt_auth_chk
					,lt_card_type,lt_card_no,lt_Auth,lt_host_code,MoleQUtil.getPaymentbyId(lt_function),lt_trn_no));
		}
		cursor.close();
		return obj;
	}
	
	public void Insert(PosTender obj){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL(
				"INSERT INTO " + DatabaseSchema.PoslineTender.SCHEMA_NAME + " VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
				new Object[] { obj.getPt_id(), obj.getPt_store_id(), obj.getPt_station(), obj.getPt_date(), obj.getPt_seq_no()
						, obj.getPt_online(), obj.getPt_function(), obj.getPt_due(), obj.getPt_tender(),
						obj.getPt_change(), obj.getPt_ref_no(), obj.getPt_auth_chk(),
						obj.getPt_card_type(),obj.getPt_card_no(),obj.getPt_Auth(),obj.getHostCode(),obj.getPt_trn_no()});
	}
	
	public int getMaxSeqNo(){
		int i = 0;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		
		Cursor cursor = db.rawQuery("select Max(lt_seq_no) from " + DatabaseSchema.PoslineTender.SCHEMA_NAME, null);
		if (cursor == null) {
			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return 0;
		}	
		
		if (cursor.moveToNext()) {
			i = cursor.getInt(0);
		}
		cursor.close();
		return i;
	}
	
	public int getMaxNo(){
		int i = 0;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		
		Cursor cursor = db.rawQuery("select Max(lt_id) from " + DatabaseSchema.PoslineTender.SCHEMA_NAME, null);
		if (cursor == null) {
			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return 0;
		}	
		
		if (cursor.moveToNext()) {
			i = cursor.getInt(0);
		}
		cursor.close();
		return i;
	}
	
	public void ShowMessage(String text, int type, int icon) {
		mMessageDialog = new MessageDialog(context);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}
	
	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener() {

		@Override
		public void onClickYes() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onClickNo() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onClickOK() {
			// TODO Auto-generated method stub
			mMessageDialog.dismiss();
		}
	};
}
